package com.matt.repository.cookbook;

import com.matt.domain.cookbook.Recipe;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Repository
public interface RecipeRepository extends JpaRepository<Recipe, Long>, JpaSpecificationExecutor<Recipe> {

    @Transactional
    @Modifying
    @Query(value = "DELETE FROM tb_recipe WHERE food_id = ?1 AND recipe_type_id = ?2 AND DATE_FORMAT(recipe_date,'%Y-%m-%d') = ?3 AND area_id = ?4 ",nativeQuery = true)
    void deleteBy(long foodId,long recipeTypeId,String recipeDate,String areaId);

    @Transactional
    void deleteByAreaIdAndRecipeTypeIdAndRecipeDateGreaterThan(String areaId, Long recipeTypeId,  Date date);
    List<Recipe> findAllByAreaIdAndRecipeDateBetweenOrderByRecipeDateAsc(String areaId, Date startDate, Date endDate);

    List<Recipe> findAllByAreaIdAndRecipeDateBetweenOrderById(String areaId, Date startDate, Date endDate);

    Recipe findByAreaIdAndId(String areaId, Long id);
}